<template>
  <div class="layout-container">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="logo-container">
        <img src="@/assets/imgs/logo.png" alt="" class="logo">
        <div class="title">党团信息管理系统</div>
      </div>
      <div class="user-info">
        <span class="user-type">{{ getUserType(data.user.userType) }}</span>
        <el-dropdown @command="handleCommand">
          <div class="user-dropdown-link">
            <img class="avatar"
                 :src="data.user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"
                 alt="">
            <span class="username">{{ data.user.name }}</span>
            <el-icon class="el-icon--right"><CaretBottom /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="person">
                <el-icon><User /></el-icon>个人资料
              </el-dropdown-item>
              <el-dropdown-item command="password">
                <el-icon><Lock /></el-icon>修改密码
              </el-dropdown-item>
              <el-dropdown-item divided command="logout">
                <el-icon><SwitchButton /></el-icon>退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <div class="main-container">
      <!-- 侧边栏 -->
      <div class="sidebar">
        <!-- 在模板中修改菜单部分 -->
        <el-menu
            router
            class="sidebar-menu"
            :default-active="router.currentRoute.value.path"
            :default-openeds="['/home', '2']"
        >
          <el-menu-item index="/home">
            <el-icon><HomeFilled/></el-icon>
            <span>系统首页</span>
          </el-menu-item>
          <el-sub-menu index="2" v-if="data.user.userType === 6">
            <template #title>
              <el-icon><Memo/></el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/sysUser">
              <el-icon><User/></el-icon>
              <span>用户信息</span>
            </el-menu-item>
          </el-sub-menu>
          <el-menu-item index="/studyMaterial" v-if="[1,2,4,5].includes(data.user.userType)">
            <el-icon><Printer/></el-icon>
            <span>学习资料</span>
          </el-menu-item>
          <el-menu-item index="/membershipFee" v-if="[1,2,3,4,5].includes(data.user.userType)">
            <el-icon><Money/></el-icon>
            <span>党团缴费</span>
          </el-menu-item>
          <el-menu-item index="/relationshipTransfer" v-if="[2,3,5].includes(data.user.userType)">
            <el-icon><Link/></el-icon>
            <span>组织关系转接</span>
          </el-menu-item>
          <el-menu-item index="/organizationMember" v-if="[3].includes(data.user.userType)">
            <el-icon><User/></el-icon>
            <span>组织成员管理</span>
          </el-menu-item>
          <el-menu-item index="/statisticalAnalysis" v-if="[].includes(data.user.userType)">
            <el-icon><User/></el-icon>
            <span>组织成员统计</span>
          </el-menu-item>
          <el-menu-item index="/createOrganizationBranch" v-if="[1].includes(data.user.userType)">
            <el-icon><School/></el-icon>
            <span>创建党团支部</span>
          </el-menu-item>
          <el-menu-item index="/organizationActivity" v-if="[1,2].includes(data.user.userType)">
            <el-icon><Position/></el-icon>
            <span>组织活动管理</span>
          </el-menu-item>
          <!-- 只有教师和学生可见 -->
          <el-menu-item index="/showActivities" v-if="[3,4,5].includes(data.user.userType)">
            <el-icon><Select/></el-icon>
            <span>报名活动</span>
          </el-menu-item>
          <el-menu-item index="/myActivities" v-if="[3,4,5].includes(data.user.userType)">
            <el-icon><CircleCheck/></el-icon>
            <span>我的活动</span>
          </el-menu-item>
        </el-menu>

      </div>

      <!-- 主要内容区域 -->
      <div class="content">
        <router-view @updateUser="updateUser"/>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive } from "vue";
import router from "@/router";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  HomeFilled,
  Memo,
  User,
  Position,
  Select,
  CircleCheck,
  Lock,
  SwitchButton,
  CaretBottom
} from '@element-plus/icons-vue'
import {getUserInfo, removeUserInfo} from "@/utils/auth";

const data = reactive({
  user: getUserInfo() || {}
})

if (!data.user?.id) {
  ElMessage.error('登录信息失效，请重新请登录！')
  router.push('/login')
}

const updateUser = () => {
  data.user = getUserInfo() || {}
}

const getUserType = (type) => {
  const types = {
    1: '校团委书记',
    2: '院团委书记',
    3: '班级团委书记',
    4: '教师',
    5: '学生',
    6: '系统管理员'
  }
  return types[type] || '未知'
}

const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm(
        '确定要退出系统吗？',
        '退出提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
    ).then(() => {
      ElMessage.success('退出成功')
      removeUserInfo()
      router.push('/login')
    }).catch(() => {})
  } else {
    router.push(`/${command}`)
  }
}
</script>

<style scoped>
.layout-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  height: 60px;
  background: linear-gradient(90deg, #c8161d 0%, #e54b4b 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  color: white;
  position: relative;
  z-index: 1000;
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  width: 40px;
  height: 40px;
  object-fit: contain;
}

.title {
  font-weight: bold;
  font-size: 22px;
  margin-left: 12px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
  letter-spacing: 1px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-type {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.9);
  background: rgba(255, 255, 255, 0.1);
  padding: 4px 8px;
  border-radius: 4px;
}

.user-dropdown-link {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.8);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.username {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.9);
}

.main-container {
  display: flex;
  min-height: calc(100vh - 60px);
  position: relative;
}

.sidebar {
  width: 220px;
  background-color: white;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 900;
}

.sidebar-menu {
  border: none;
  padding: 10px 0;
}

.content {
  flex: 1;
  padding: 24px;
  background-color: #f8f8ff;
  min-height: calc(100vh - 60px);
  position: relative;
}

/* Element Plus 组件样式覆盖 */
:deep(.el-menu-item.is-active) {
  background-color: #fff1f0 !important;
  color: #c8161d !important;
  border-right: 3px solid #c8161d;
  font-weight: 500;
}

:deep(.el-menu-item:hover) {
  background-color: #fff1f0 !important;
  color: #c8161d !important;
}

:deep(.el-sub-menu__title:hover) {
  background-color: #fff1f0 !important;
}

:deep(.el-menu-item) {
  border-radius: 4px;
  margin: 4px 8px;
  height: 50px;
  line-height: 50px;
  transition: all 0.3s ease;
}

:deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
}

:deep(.el-menu-item .el-icon),
:deep(.el-sub-menu__title .el-icon) {
  margin-right: 12px;
  font-size: 18px;
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 4px;
}

:deep(.el-dropdown-menu__item:not(.is-disabled):hover) {
  background-color: #fff1f0;
  color: #c8161d;
}

:deep(th) {
  color: #333;
  background-color: #fafafa;
  font-weight: 500;
}

:deep(.el-card) {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-button--primary) {
  background-color: #c8161d;
  border-color: #c8161d;
}

:deep(.el-button--primary:hover) {
  background-color: #d4262d;
  border-color: #d4262d;
}

:deep(.el-form-item) {
  margin-bottom: 22px;
}

:deep(.el-input) {
  border-radius: 4px;
}

@media screen and (max-width: 768px) {
  .sidebar {
    width: 64px;
  }

  .content {
    padding: 16px;
  }

  .title {
    display: none;
  }
}
</style>
